<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-row">
            <div class="layui-col-md3 layui-col-sm18">
                <table id="login-log-table" class="layui-table" lay-filter="login-log-table"></table>
                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-xs" lay-event="table">查看</a>
                </script>
            </div>
            <div class="layui-col-md2 layui-col-sm6" style="margin-top:20px;margin-right: 10px">
                    <div class="layui-card">
                        <div class="layui-card-header"><span id="show_table"></span> 表的字段</div>
                        <div class="layui-card-body" id="tables">
                            点击左侧查看按钮查看该表的字段
                        </div>
                    </div>
            </div>
            <div class="layui-col-md4 layui-col-sm12">
                <div class="layui-card" style="margin-top:20px">
                    <div class="layui-card-header">运行sql(如果有返回将在内容返回区呈现)</div>
                      <div class="layui-card-body">
                          <div class="layui-form">
                            <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">SQL</label>
                                <div class="layui-input-block">
                                    <textarea name="sql" style="height: 250px"
                                              placeholder="输入sql语名，如果有返回，内容将返回在下面的输入框里" class="layui-textarea"></textarea>
                                </div>
                            </div>
                              <div class="layui-form-item">
                                  <label class="layui-form-label"></label>
                                  <div class="layui-form-block">
                                      <button class="layui-btn  layui-btn-danger" id="run-sql">
                                          提交
                                      </button>
                                  </div>
                              </div>
                              <div class="layui-form-item">
                                  内容返回区:
                              </div>
                              <div class="layui-form-item">
                                  <div id="return" class="layui-card-body">当前无返回结果</div>
                              </div>
                         </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script>

    tb = table.render({
        elem: '#login-log-table',
        url: '/admin/set/sql.htm',
        width:410,
        cellMinWidth: 120,
        limit:25,
        cols: [[
            {field: 'Tables_in_admin_role', title: '名称'},
            {title: '操作',  width: 120, templet: '#currentTableBar',  align: "center"}
        ]],
    });
    table.on('tool(login-log-table)', function (obj) {
        const data = obj.data;
        let html = ' ';
        if( obj.event === 'table'){
            http.post('/admin/set/sql.htm',{t:'tables',table:data.Tables_in_admin_role}).done( res => {
                const d = res.result;
                $('#show_table').text(data.Tables_in_admin_role)
                d.map( (v) => {
                    html += '<p class="layui-text layui-form-item" style="font-size:1rem;font-weight: bold">'
                            +v.Field + ' => '+v.Type+'</p>'
                })
                $('#tables').html(html)
            })
        }
    })
    $('#run-sql').click(function () {
        const sql = $('textarea[name="sql"]').val()
        if(sql){
            $(this).attr('disabeld',true).addClass('layui-btn-disabled')
            http.post('/admin/set/sql.htm',{t:sql}).done(res => {
                layer.msg(res.msg ? res.msg : '操作成功！',{time:1000})
                layuimini.jsonViewer(res.result,{},'#return')
                $(this).attr('disabled',false).removeClass('layui-btn-disabled')
            }).fail( () =>{
                $(this).attr('disabled',false).removeClass('layui-btn-disabled')
            })
        }
    })
</script>